use crate::plan_struct::Tag;
use std::fmt::Display;
use std::sync::Arc;
use time::{OffsetDateTime, UtcOffset};

impl Tag {
    pub fn new_string(s: String) -> Self {
        Tag::String(Arc::new(s))
    }
    pub fn new_utc_plus_8() -> Self {
        let utc_now = OffsetDateTime::now_utc();
        let beijing_offset = UtcOffset::from_hms(8, 0, 0).expect("无效的UTC偏移");
        Tag::Date(utc_now.to_offset(beijing_offset))
    }
    pub fn new_beijing_now() -> Self {
        Self::new_utc_plus_8()
    }
    pub fn is_string(&self) -> bool {
        matches!(self, Tag::String(_))
    }
    pub fn is_date(&self) -> bool {
        matches!(self, Tag::Date(_))
    }
    pub fn take_string(self) -> Option<String> {
        match self {
            Tag::String(s) => Some(s.to_string()),
            _ => None,
        }
    }
}
impl Display for Tag {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Tag::String(s) => write!(f, "{s}"),
            Tag::Date(d) => write!(f, "{d}"),
        }
    }
}
